<template>
  <Layout title="登陆" :has-back="false" :has-tabbar="false">
    <view
      style="
        height: 100%;
        width: 100%;
        background-size: 100% 100%;
        display: flex;
        flex-direction: column;
        justify-content: center;
      "
      :style="{ backgroundImage: `url(${loginImg})` }"
    >
      <view style="margin-left: 50rpx; margin-right: 50rpx">
        <u-form
          :labelStyle="{ color: '#2b487d', fontSize: '40rpx' }"
          label-width="150rpx"
        >
          <u-form-item label="账号">
            <u-input
              v-model="formData.username"
              :customStyle="{ border: '2rpx solid #2b487d !important' }"
              :clearable="true"
            ></u-input>
          </u-form-item>
          <u-form-item label="密码">
            <u-input
              type="password"
              v-model="formData.password"
              :customStyle="{ border: '2rpx solid #2b487d !important' }"
              :clearable="true"
            ></u-input>
          </u-form-item>
          <u-form-item label=" ">
            <u-button type="primary" @click="login">
              <text style="font-size: 40rpx">登陆</text>
            </u-button>
          </u-form-item>
        </u-form>
      </view>
    </view>
  </Layout>
</template>

<script>
import { login } from "@/api/login";
import { getUser } from "@/api/user";
import Layout from "@/pages/component/layout/index.vue";
import { showErrorToast } from "@/util/message";
import { getVersion } from "@/api/version";

export default {
  name: "Login",
  components: {
    Layout,
  },
  mounted() {
    this.isReal().then((isReal) => {
      if (isReal) {
        this.loginImg = `${process.env.VUE_APP_API_BASE_URL}/static/admin/login.jpeg`;
      } else {
        this.loginImg = `${process.env.VUE_APP_API_BASE_URL}/static/admin/login1.jpeg`;
      }
    });
  },
  data() {
    return {
      loginImg: "",
      formData: {
        username: "",
        password: "",
      },
    };
  },
  computed: {
    store() {
      return this.$store.state;
    },
  },
  methods: {
    async isReal() {
      let res = await getVersion();
      let resIsReal = res.data.isReal;
      let envVersion = wx.getAccountInfoSync().miniProgram.envVersion;
      let isReal = false;
      if (envVersion === "trial" && resIsReal) {
        isReal = true;
      } else if (envVersion === "trial" && !resIsReal) {
        isReal = false;
      } else if (envVersion !== "trial") {
        isReal = true;
      }
      return isReal;
    },
    async login() {
      try {
        let result = await login(this.formData);
        let token = result.data.token;
        this.$store.commit("setToken", token);
        result = await getUser({ token });
        let user = result.data;
        let { name, avatar, roleList } = user;
        this.$store.commit("setName", name);
        this.$store.commit("setAvatar", avatar);
        this.$store.commit("setRoleList", roleList);
        let isReal = await this.isReal();
        if (isReal) {
          await uni.reLaunch({
            url: "/sub/owner/index",
          });
        } else {
          await uni.reLaunch({
            url: "/pages/center/index",
          });
        }
      } catch (error) {
        showErrorToast(error);
      }
    },
  },
};
</script>
